//
//  HeartBarData.swift
//  DaRings
//
//  Created by 尹琼 on 2023/2/17.
//

import UIKit

public struct HeartBarDataValue {
    
    public var high: Double
    public var low: Double
    
    
    public var array: [Double] {
        return [low, high]
    }
    
    public var tuple: (low: Double, high: Double) {
        return (low, high)
    }
    
    public var isEmpty: Bool {
        return high == 0 && low == 0
    }
    
    static public var zero: HeartBarDataValue {
        
        return HeartBarDataValue(high: 0, low: 0)
    }
}

public struct HeartBarData {
    
    public var dayTime: HeartBarDataValue
    public var sleeping: HeartBarDataValue
    public var training: HeartBarDataValue
    
    /// 这一段数据的开始时间戳
    public var startTime: TimeInterval
    /// 这一段数据的结束时间戳
    public var endTime: TimeInterval
    /// 平均值
    public var avg: Int
    
    /// 这一段3色中最大的值
    public var max: Int
    /// 这一段3色中最小的
    public var min: Int
    
    public var array: [HeartBarDataValue] {
        return [training, sleeping, dayTime]
    }
    
    
    public var barData: [[Double]] {
        
        return [training.array, sleeping.array, dayTime.array]
    }
    
    
    
    public init(dayTime: HeartBarDataValue, sleeping: HeartBarDataValue, workouts: HeartBarDataValue, startTime: TimeInterval, endTime: TimeInterval, avg: Int) {
        
        self.dayTime = dayTime
        self.sleeping = sleeping
        self.training = workouts
        self.startTime = startTime
        self.endTime = endTime
        self.avg = avg
        
        let barDatas = [workouts.array, dayTime.array, sleeping.array].flatMap { $0 }
        var max: Double = 0
        var min: Double = 255
        
        max = barDatas.max() ?? 0
        min = barDatas.filter({ $0 > 0 }).min() ?? 255
        
        if min > max {
            min = max
        }
        
        //            print(">>>")
        //            print(max)
        //            print(min)
        //            print(">>>")
        
        self.max = Int(max)
        self.min = Int(min)
        
        
    }
    
}
